package net.tygzs.minerpg.Configuration.Item;

import java.util.ArrayList;
import java.util.List;
import me.dpohvar.powernbt.api.NBTCompound;
import me.dpohvar.powernbt.api.NBTList;
import net.tygzs.minerpg.Main;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author thtTNT
 */
public class RPGItem
{

    /**
     * @return the type
     */
    public RPGItemType getType()
    {
        return type;
    }

    /**
     * @param type the type to set
     */
    public void setType(RPGItemType type)
    {
        this.type = type;
    }

    /**
     * @return the FixedArmor
     */
    public int getFixedArmor()
    {
        return FixedArmor;
    }

    /**
     * @param FixedArmor the FixedArmor to set
     */
    public void setFixedArmor(int FixedArmor)
    {
        this.FixedArmor = FixedArmor;
    }

    /**
     * @return the SectionalArmor
     */
    public double getSectionalArmor()
    {
        return SectionalArmor;
    }

    /**
     * @param SectionalArmor the SectionalArmor to set
     */
    public void setSectionalArmor(double SectionalArmor)
    {
        this.SectionalArmor = SectionalArmor;
    }
    
    public enum RPGItemType
    {
        
        Normal, Helmet, Chestplate, Leggings, Boots;
        
        public static RPGItemType getRPGItemType(String name)
        {
            if (name.equalsIgnoreCase("Normal"))
            {
                return Normal;
            }
            if (name.equalsIgnoreCase("Helmet"))
            {
                return Helmet;
            }
            if (name.equalsIgnoreCase("Chestplate"))
            {
                return Chestplate;
            }
            if (name.equalsIgnoreCase("Leggings"))
            {
                return Leggings;
            }
            if (name.equalsIgnoreCase("Boots"))
            {
                return Boots;
            }
            return null;
        }
        
        public static String getName(RPGItemType type)
        {
            if (type == RPGItemType.Normal)
            {
                return "普通";
            }
            if (type == RPGItemType.Helmet)
            {
                return "头盔";
            }
            if (type == RPGItemType.Chestplate)
            {
                return "胸甲";
            }
            if (type == RPGItemType.Leggings)
            {
                return "护腿";
            }
            if (type == RPGItemType.Boots)
            {
                return "鞋子";
            }
            return null;
        }
    }
    
    private int damage;
    private int MaxDur;
    private int Dur;
    private String disPlayName;
    private int disPlayID;
    private short disPlayDur;
    private List<String> Lores;
    private RPGItemType type;
    private double SectionalArmor = 0.0;
    private int FixedArmor = 0;

    /**
     * @return the damage
     */
    public int getDamage()
    {
        return damage;
    }

    /**
     * @param damage the damage to set
     */
    public void setDamage(int damage)
    {
        this.damage = damage;
    }

    /**
     * @return the dur
     */
    public int getMaxDur()
    {
        return MaxDur;
    }

    /**
     * @param dur the dur to set
     */
    public void setMaxDur(int dur)
    {
        this.MaxDur = dur;
    }

    /**
     * @return the disPlayName
     */
    public String getDisPlayName()
    {
        return disPlayName;
    }

    /**
     * @param disPlayName the disPlayName to set
     */
    public void setDisPlayName(String disPlayName)
    {
        this.disPlayName = disPlayName;
    }

    /**
     * @return the disPlayID
     */
    public int getDisPlayID()
    {
        return disPlayID;
    }

    /**
     * @param disPlayID the disPlayID to set
     */
    public void setDisPlayID(int disPlayID)
    {
        this.disPlayID = disPlayID;
    }
    
    public static RPGItem init(ItemStack is)
    {
        if (is == null)
        {
            return null;
        }
        NBTCompound nbt = Main.getPluginConfig().getNBTManager().read(is);
        if (nbt == null)
        {
            return null;
        }
        if (!nbt.containsKey("RPGItem"))
        {
            return null;
        }
        RPGItem ri = new RPGItem();
        ri.setDamage(nbt.getInt("Damage"));
        ri.setDisPlayName(is.getItemMeta().getDisplayName());
        ri.setDisPlayID(is.getTypeId());
        ri.setDisPlayDur(is.getDurability());
        ri.setMaxDur(nbt.getInt("MaxDur"));
        ri.setDur(nbt.getInt("Dur"));
        if (nbt.containsKey("FixedArmor"))
        {
            ri.setFixedArmor(nbt.getInt("FixedArmor"));
        } else
        {
            ri.setFixedArmor(0);
        }
        if (nbt.containsKey("Type"))
        {
            ri.setType(RPGItemType.getRPGItemType(nbt.getString("Type")));
        } else
        {
            ri.setType(RPGItemType.Normal);
        }
        if (nbt.containsKey("SectionalArmor"))
        {
            ri.setSectionalArmor(nbt.getDouble("SectionalArmor"));
        }
        NBTList nl = nbt.getList("Lores");
        if (nl != null)
        {
            List<Object> list = nl.toArrayList();
            List<String> lores = new ArrayList<>();
            for (Object value : list)
            {
                lores.add((String) value);
            }
            ri.setLores(lores);
        }
        return ri;
    }
    
    public static ItemStack BuildItem(RPGItem ri)
    {
        ItemStack is = new ItemStack(ri.getDisPlayID(), 1, ri.getDisPlayDur());
        NBTCompound nbt = new NBTCompound();
        List<String> lores = new ArrayList<>();
        if (ri.getDur() == 0)
        {
            return new ItemStack(Material.AIR);
        }
        if (ri.getDamage() != 0)
        {
            nbt.put("Damage", ri.getDamage());
            lores.add(ChatColor.YELLOW + "物品伤害:" + ri.getDamage());
        }
        if (ri.getMaxDur() != 0)
        {
            nbt.put("MaxDur", ri.getMaxDur());
            nbt.put("Dur", ri.getDur());
            lores.add(ChatColor.GOLD + "耐久度:" + ri.getDur() + "/" + ri.getMaxDur());
        }
        NBTList nl = new NBTList();
        if (ri.getLores() != null)
        {
            for (String lore : ri.getLores())
            {
                nl.add(lore);
                lores.add(lore.replace("&", "§"));
            }
        }
        nbt.put("Lores", nl);
        nbt.put("RPGItem", true);
        nbt.put("Type", ri.getType().name());
        nbt.put("FixedArmor", ri.getFixedArmor());
        nbt.put("SectionalArmor", ri.getSectionalArmor());
        lores.add(ChatColor.BLUE + "物品类型:" + RPGItemType.getName(ri.getType()));
        if (ri.getType() == RPGItemType.Helmet || ri.getType() == RPGItemType.Chestplate || ri.getType() == RPGItemType.Leggings || ri.getType() == RPGItemType.Boots)
        {
            lores.add(ChatColor.BLUE + "右键即可装备此物品");
        }
        Main.getPluginConfig().getNBTManager().write(is, nbt);
        ItemMeta im = is.getItemMeta();
        im.setLore(lores);
        im.setDisplayName(ri.getDisPlayName().replace("&", "§"));
        is.setItemMeta(im);
        return is;
    }

    /**
     * @return the Dur
     */
    public int getDur()
    {
        return Dur;
    }

    /**
     * @param Dur the Dur to set
     */
    public void setDur(int Dur)
    {
        this.Dur = Dur;
    }

    /**
     * @return the Lores
     */
    public List<String> getLores()
    {
        return Lores;
    }

    /**
     * @param Lores the Lores to set
     */
    public void setLores(List<String> Lores)
    {
        this.Lores = Lores;
    }

    /**
     * @return the disPlayDur
     */
    public short getDisPlayDur()
    {
        return disPlayDur;
    }

    /**
     * @param disPlayDur the disPlayDur to set
     */
    public void setDisPlayDur(short disPlayDur)
    {
        this.disPlayDur = disPlayDur;
    }
}
